#!/bin/bash

__FileName__=""
__Author__="SuoSuo"
__Time__="/2023/07/23"
__Version__="V1.6"



#显示程序的版本号和更新日期
version () {
	printf "\033[34m
=============================================================
*   ${PROGRAM_NAME} ${PROGRAM_VERSION}
*   ${AUTHOR_NAME}          ${AUTHOR_NUMBER}
=============================================================
Release Note:
    >>V0.4 20230701 is first commit
    1. show infomation for network adapter
    >>V0.7 20230703
    1. modify query network interface name
    >>V1.0 20230704
    >>V1.3 20230705
    >>V1.4 20230709
    >>V1.5 20230720
    >>V1.6 20230723

---------------------------------------------------------------
    \033[0m\n"
	exit 0
}


# 错误输出的处理方式
error () {
	printf "\033[31m$PROGRAM_NAME run with error at $date_time:\033[0m $1\n"
	exit 1
}
# 1.0.4新增函数，判断是不是root用户
check_user_is_root () {
    username=$(whoami)
    if [ "$username" != "root" ]
    then
        error  "Username is must be root"
        exit 1
    fi
}
# 获取实时时间
gdate () {
    echo $(date +%Y.%m.%d.%H.%M.%S)
}

show_brief_network () {
    printf '[root@Num_eigth_46 ~]#\033[32m lspci -v | grep -i  ether\033[0m\n'

    lspci -v 2> /dev/null | grep -i ether

    printf "\033[34m%-12s %-7s %-14s %-13s %-16s %-22s%5s%5s%6s%10s\033[0m\n"  "  Name" " Status"  "     Bus"  "DriName"  "DriVer" "         FwVer"  "Slot" "Numa" "VD" "SubVD"
    for i in `cat /proc/net/dev | egrep -iv '^[ ]*lo|virbr|vnet' |sed '3,$p' -n | cut -d: -f1 | sort`
    do
        printf "%-16s" $i
        state=`ip addr  show $i | sed '1p' -n  | awk -F state '{print $2}' | awk '{print $1}'`
        printf "%-5s" $state
        bus=`ethtool -i $i | grep -i bus | awk -F info: '{print $2}'`
        printf "%-13s" $bus
        driver_name=`ethtool -i $i | sed '1p' -n | awk -F river: '{print $2}'`
        printf "%-14s" $driver_name
        driver_ver=`ethtool -i $i | sed '2p' -n | awk -F sion: '{print $2}'`
        printf "%-17s" $driver_ver
        fw_version=`ethtool -i $i | sed '3p' -n | cut -d: -f2`
        printf "%-28s" "$fw_version"
        ids_str=`lspci -nvmms ${bus} 2> /dev/null`
        if [ $? -ne 0 ]; then
            printf "\n"
            continue
        fi
    #    slots=`lspci -v -s ${bus} 2> /dev/null | grep 'Physical Slot' | awk '{print $NF}'`
        slots=`echo ${ids_str} | awk -F 'PhySlot' '{print $2}' | awk '{print $2}' | xargs`
        printf "%-3s" "${slots}"
        numanode=`echo ${ids_str} | awk -F 'NUMANode' '{print $2}' | awk '{print $2}' | xargs`
        printf "%-3s" "${numanode}"
        vendor=`echo ${ids_str} | awk -F 'Vendor' '{print $2}' | awk '{print $2}' | xargs`
        device=`echo ${ids_str} | awk -F 'Device' '{print $2}' | awk '{print $2}' | xargs`
        printf "%-10s" "${vendor}:${device}"
        svendor=`echo ${ids_str} | awk -F 'SVendor' '{print $2}' | awk '{print $2}' | xargs`
        sdevice=`echo ${ids_str} | awk -F 'SDevice' '{print $2}' | awk '{print $2}' | xargs`
        printf "%-10s\n" "${svendor}:${sdevice}"
    done

    #lspci -vvv -s 0000:31:00.0 | grep 'Physical Slot' | cut -d ':' -f2

}

show_brief_fc () {
    echo

}

show_brief_gpu () {
    echo

}

show_brief_storage () {
    echo

}

show_brief_disk () {
    echo

}


#当前时间，格式是“年.月.日.时.分.秒”
date_time=`date +%Y.%m.%d.%H.%M.%S`
#文件名称
PROGRAM_NAME=$(basename $0)
#作者名字
AUTHOR_NAME="SuoSuo"
#作者工号
AUTHOR_NUMBER="50023"
# 版本
PROGRAM_VERSION="V1.6"
# 首地址
BASE_DIR=`pwd`
SHOW_VERBOSE=0

IS_NETWORK_SHOW=false
IS_FC_SHOW=false
IS_STORAGE_SHOW=false
IS_GPU_SHOW=false
IS_DISK_SHOW=false


check_user_is_root
#显示帮助目录菜单
usage_and_exit () {
    echo
    printf "${PROGRAM_NAME} Version ${PROGRAM_VERSION}\n"
	printf "\\033[32m
    Brief information of status and configuration for Every Devices
Usage:
  $PROGRAM_NAME [options]
Options:
    -n           Brief information of status and configuration for Network Adapter(s)
    -f           Brief information of status and configuration for FC Adapter(s)
    -s           Brief information of status and configuration for Storage Adapter(s)
    -g           Brief information of status and configuration for Graphical Adapter(s)
    -d           Brief information of status and configuration for Disk(s)
    -v           Brief information of status be Detailes
    -e           show $PROGRAM_NAME versions
    -h           show help message
    \n\
Attention:
    1>.
    2>.
Examples:
    ./${PROGRAM_NAME} -f 44:00.0
    ./${PROGRAM_NAME} -d
    ./${PROGRAM_NAME} -m
    ./${PROGRAM_NAME} -d -f 44:00.0
Questions:
    1>.
    2>.   \
    \033[0m\n
"
}

while getopts "nfsgdveh" arg
do
	case $arg in
		n)
		IS_NETWORK_SHOW=true;;
		f)
		IS_FC_SHOW=true;;
		s)
		IS_STORAGE_SHOW=true;;
		g)
		IS_GPU_SHOW=true ;;
		d)
		IS_DISK_SHOW=true ;;
	    v)
	    let SHOW_VERBOSE++ ;;
	    e)
	    version;;
	    h)
		usage_and_exit;;
	esac
done

if [[ $# -eq 0 ]];then
	usage_and_exit
	exit 1
fi
if ${IS_NETWORK_SHOW};then
	show_brief_network
fi
if ${IS_FC_SHOW};then
	show_brief_fc
fi
if ${IS_STORAGE_SHOW};then
	show_brief_storage
fi
if ${IS_GPU_SHOW};then
	show_brief_gpu
fi
if ${IS_DISK_SHOW};then
	show_brief_disk
fi